#include <QDebug>
#include <QtConcurrentMap.h>
#include "mainwindow.h"

double lowLimit = 0;
double upLimit  = 1;
double step     = 0.1;

double function(double x){
    return sin(x);
}

struct Chunk{
private:
    double m_x1;
    double m_x2;
public:
    Chunk(double x1, double x2) : m_x1(x1), m_x2(x2){ }
    double x1() const { return m_x1; }
    double x2() const { return m_x2; }
};

double mapFunction(const Chunk &chunk){
    double x1 = chunk.x1();
    double x2 = chunk.x2();
    return (x2 - x1) * abs(function((x1 + x2)/2));
}

void reduceFunction(double &result, const double &intermediate){
    result += intermediate;
}

int main(int argc, char *argv[])
{

    QList<Chunk> chunks;
    for(double i = lowLimit; i < upLimit - step; i += step){
        chunks.append(Chunk(i, i + step));
    }

    double result = QtConcurrent::blockingMappedReduced(chunks, mapFunction, reduceFunction);

    qDebug()<<"result = "<<result;

    return 0;
}
